package org.example.questionnairebackend.service.Impl;

import org.example.questionnairebackend.Data.Options;
import org.example.questionnairebackend.Data.Questions;
import org.example.questionnairebackend.Data.Submissions;
import org.example.questionnairebackend.Data.Surveys;
import org.example.questionnairebackend.Dto.OptionDTO;
import org.example.questionnairebackend.Dto.QuestionDTO;
import org.example.questionnairebackend.Dto.QuestionOptionDTO;
import org.example.questionnairebackend.Vo.AddVo;
import org.example.questionnairebackend.Vo.QuestionCreateRequest;
import org.example.questionnairebackend.Vo.SurveyCreateRequest;
import org.example.questionnairebackend.mapper.OptionsMapper;
import org.example.questionnairebackend.mapper.QuestionsMapper;
import org.example.questionnairebackend.mapper.SubmissionsMapper;
import org.example.questionnairebackend.mapper.SurveysMapper;
import org.example.questionnairebackend.service.SurveysService;
import org.example.questionnairebackend.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SurveysServiceImpl implements SurveysService {
    @Autowired
    private SurveysMapper surveysMapper;
    @Autowired
    private SubmissionsMapper submissionsMapper;
    @Autowired
    private QuestionsMapper questionsMapper;
    @Autowired
    private OptionsMapper optionsMapper;

    @Override
    public List<Surveys> getAllSurveys() {
        return surveysMapper.getAllSurveys();
    }

    @Override
    public List<QuestionDTO> getSurveyQuestions(Integer surveyId) {
        List<QuestionOptionDTO> questionOptionList = surveysMapper.getQuestionsWithOptions(surveyId);

        Map<Integer, QuestionDTO> questionMap = new LinkedHashMap<>();
        for (QuestionOptionDTO item : questionOptionList) {
            QuestionDTO question = questionMap.computeIfAbsent(item.getQuestionId(), id -> {
                QuestionDTO q = new QuestionDTO();
                q.setQuestionId(item.getQuestionId());
                q.setQuestionText(item.getQuestionText());
                q.setQuestionType(item.getQuestionType());
                q.setOrderNum(item.getOrderNum());
                q.setOptions(new ArrayList<>());
                return q;
            });
            if (item.getOptionId() != null) {
                question.getOptions().add(new OptionDTO(item.getOptionId(), item.getOptionText()));
            }
        }

        return new ArrayList<>(questionMap.values());
    }

    @Override
    public void submitSurvey(Submissions submissions) {
        submissionsMapper.insert(submissions);
    }

    @Override
    public Integer addSurvey(Surveys surveys) {
        return surveysMapper.insert(surveys);
    }

    @Override
    public void createSurvey(SurveyCreateRequest request) {
        // 1. 插入问卷
        Surveys survey = new Surveys();
        survey.setTitle(request.getTitle());
        survey.setDescription(request.getDescription());
        survey.setUserId(1); // 当前登录用户ID
        survey.setCreatedAt(new java.util.Date());
        survey.setUpdatedAt(new java.util.Date());
        surveysMapper.insert(survey);
        Integer surveyId = survey.getId();
// 2. 插入问题和选项
        int orderNum = 1;
        for (QuestionCreateRequest q : request.getQuestions()) {
            Questions question = new Questions();
            question.setSurveyId(surveyId);
            question.setQuestionText(q.getText());
            question.setQuestionType(q.getType() == 0 ? "radio" : "text");
            question.setOrderNum(orderNum++);
            questionsMapper.insert(question);
            Integer questionId = question.getId();
            // 插入选项
            if (q.getType() == 0 && q.getOptions() != null) {
                for (String optionText : q.getOptions()) {
                    Options option = new Options();
                    option.setQuestionId(questionId);
                    option.setOptionText(optionText);
                    optionsMapper.insert(option);
                }
            }
        }
    }
}
